home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1997 July: Mac OS SDK / Dev.CD Jul 97 SDK2.toast / Development Kits (Disc 2) / ScriptX / Code Samples / untested / tcpip / ifish / animdemo.sx < prev    next >
Encoding:
Text File  |  1996-05-21  |  8.9 KB  |  405 lines  |  [TEXT/ttxt]

  1. -- Filename: 
  2. --     animdemo.sx
  3.  
  4. -- Other Files Required:
  5. --     This file is loaded by loadme.sx in the animate folder.
  6.  
  7. -- Purpose:  
  8. --    animdemo.sx defines a class AnimationDemo, a subclass of Window, which creates
  9. --    an instance of Animation and adds to the window.
  10.  
  11. -- Specialized Classes:  
  12. --     AnimationDemo
  13.  
  14. -- Instructions to User: 
  15. --    The ScriptX code "show (new AnimationDemo)" creates the window, adds an
  16. --    instance of class Animation, and shows the window.
  17.  
  18. -- Author:
  19. --     Steve Mayer
  20.  
  21. in module InternetFish
  22.  
  23. --- Edit these to set the defaults
  24.  
  25. global defaultPort := "2000"
  26. global defaultServer := "pccer"
  27.  
  28. class fishui ()
  29. instance variables
  30.     win : (new TwoDSpace boundary: (new rect x2: 200 y2: 160))
  31.     portField : (new smalltextedit text: defaultPort)
  32.     portLabel : (new label text: "Port:")
  33.     serverField : (new smalltextedit text: defaultServer)
  34.     serverLabel : (new label text: "Server:")
  35.     okButton : (new textbutton text: "ok")
  36.     client
  37.     getgoing
  38. end
  39.  
  40. function myappend x y -> (
  41.     y.stationary := false
  42.     append x y
  43. )
  44.  
  45. method init self {object fishui} #rest args #key client: getgoing: showServer: (false) -> (
  46.     apply nextmethod self args
  47.  
  48.     self.client := client
  49.     self.getgoing := getgoing
  50.  
  51.     myappend self.win self.portField
  52.     myappend self.win self.portLabel
  53.  
  54.     if showServer do (
  55.         myappend self.win self.serverLabel
  56.         myappend self.win self.serverField
  57.     )
  58.  
  59.     myappend self.win self.okButton
  60.  
  61.     local bc := new actuatorcontroller space: self.win
  62.     bc.wholespace := true
  63.     --
  64.  
  65.     self.portLabel.x := 10
  66.     self.portLabel.y := 80
  67.  
  68.     self.portField.x := 70
  69.     self.portField.y := 80
  70.  
  71.     self.serverLabel.x := 10
  72.     self.serverLabel.y := 50
  73.  
  74.     self.serverField.x := 70
  75.     self.serverField.y := 50
  76.     self.serverField.width := 120
  77.  
  78.     self.okButton.x := 80
  79.     self.okButton.y := 110
  80.     
  81.     self.okButton.activateAction := (ignore1 ignore2 -> okButtonPressed self)
  82.  
  83.     
  84.     self.win.x := (self.client.width - self.win.width) / 2
  85.     self.win.y := (self.client.height - self.win.height) / 2
  86.     prepend self.client self.win
  87. )
  88.  
  89. method okButtonPressed self {object fishui} -> (
  90.     local server := self.serverField.text
  91.     local port := self.portField.text as integer
  92.     local serverPort := port
  93.  
  94.     defaultServer := self.serverField.text
  95.     defaultPort := self.portField.text
  96.     
  97.     deleteOne self.client self.win
  98.  
  99.     self.getgoing self.client server: server serverPort: port port: port &
  100. )
  101.  
  102. class AnimationDemo (Window)
  103.  
  104. instance variables
  105.     animal
  106.     mc
  107.     mw
  108.     tcpListen
  109.     state
  110.     quitButton
  111.     disconnectButton
  112.     createButton
  113.     connectButton
  114.     
  115.     currentConnection
  116.     myCM
  117.     
  118. end
  119.  
  120. ---------------------------------------------------------------------------
  121. ---------------------------------------------------------------------------------------------------------
  122.  
  123.  
  124.  
  125. class method startDemo self {class AnimationDemo} -> (
  126.     local x := new self
  127.     show x
  128.     local ui := new fishui client: x getgoing: getgoing showserver: false
  129.     x
  130. )
  131.  
  132. method init self {class AnimationDemo} -> (
  133.     nextMethod self boundary:(new Rect x2:500 y2:300) \
  134.         name: "Internet Fish" \
  135.         fill: (new Brush color: (new RGBColor red:0 green:40 blue:100))
  136.     self.x := 40
  137.     self.y := 40
  138.     self.clock.scale := 10
  139.  
  140.     self.mc := new Movement space:self
  141.     
  142.     self.quitButton := new TextButton text: "Quit"
  143.     self.quitButton.x := 0
  144.     self.quitButton.y := 0
  145.     self.quitButton.activateAction := (ignore1 ignore2 -> userQuit self)
  146.     
  147.     append self self.quitButton
  148.  
  149.     self.disconnectButton := new TextButton text: "Disconnect"
  150.     self.disconnectButton.x := self.quitButton.width
  151.     self.disconnectButton.y := 0
  152.     self.disconnectButton.activateAction := (ignore1 ignore2 -> userDisconnect self)
  153.  
  154.     append self self.disconnectButton
  155.     
  156.     self.createButton := new TextButton text: "Create Fish"
  157.     self.createButton.activateAction := (ignore1 ignore2 -> createFish self)
  158.     self.createButton.x := self.disconnectButton.x + self.disconnectButton.width
  159.     
  160.     append self self.createButton
  161.     
  162.     
  163.     self.connectButton := new TextButton text: "Connect"
  164.     self.connectButton.activateAction := (ignore1 ignore2 -> connectCallback self)
  165.     self.connectButton.x := self.createButton.x + self.createButton.width
  166.     
  167.     append self self.connectButton
  168.     
  169.     local bc := new actuatorcontroller space: self
  170.     bc.wholespace := true
  171.  
  172.     setState self @ui
  173. )
  174.  
  175. method getgoing self {object animationdemo} #key server: serverport: port: -> (
  176. /*
  177.     guard (
  178. */    
  179.  
  180.         local f := new Fish authorData:self
  181.         f.x := self.width
  182.         f.y := 70
  183.         
  184.         append self f
  185.         append self.mc f
  186.  
  187.         self.mw := new Wrapper space:self client: self state: @standalone
  188.         append self.mw f
  189.         local d := new DragController space:self
  190.         append d f
  191.         self.animal := f
  192.  
  193.         self.myCM := new connectionManager client: self port: port
  194.         
  195.         return self
  196.     )
  197.  
  198. /*
  199.     catching        
  200.     TCPException: ( caught (new fishui client: self getgoing: getgoing showserver: false)
  201.             )
  202.     end
  203. */
  204.  
  205. /*
  206. )
  207. */
  208.  
  209.  
  210. -- Method prepareDrag is invoked when an object is grabbed. Remove
  211. -- the object from the movement controller.
  212. method prepareDrag self {class AnimationDemo} obj ->
  213. (
  214.     deleteOne self.mc obj
  215. )
  216.  
  217. -- Method drop is invoked when an object is dropped. Add the object
  218. -- to the movement controller.
  219. method drop self {class AnimationDemo} obj ->
  220. (
  221.     if self.mw = @own and not (ismember self.mc obj) do
  222.         appendnew self.mc obj
  223. )
  224.  
  225. --- transitions
  226.  
  227. method chooseCurrentConnection self {class AnimationDemo} -> (
  228.     self.currentConnection := chooseConnection self.myCM
  229. )
  230.  
  231. method loseCurrentConnection self {class AnimationDemo} -> (
  232.     self.currentConnection := undefined
  233. )
  234.  
  235. method ensureConnection self {object animationdemo} -> (
  236.     if self.currentconnection != undefined then return true
  237.     chooseCurrentConnection self
  238.     if self.currentConnection = undefined then (
  239.         self.mw.state := @standalone
  240.         return false
  241.     ) else (
  242.         self.mw.state := @own
  243.         return true
  244.     )
  245. )
  246.  
  247. method noteLosing self {object AnimationDemo} -> (
  248.     if  ensureConnection self do
  249.         writePacket self @losing self.animal.x
  250. )
  251.  
  252. method noteLost self {object AnimationDemo} -> (
  253.     if ensureConnection self do (
  254.         writePacket self @lost self.animal.x
  255.         deleteOne self.mc self.animal
  256.         loseCurrentConnection self
  257.     )
  258. )
  259.  
  260.  
  261. method noteStillLosing self {object AnimationDemo} -> (
  262.     if ensureConnection self do
  263.         writePacket self @stilllosing self.animal.x
  264. )
  265.  
  266. method noteGotback self {object AnimationDemo} -> (
  267.     if ensureConnection self do (
  268.         writePacket self @gotback 0
  269.         loseCurrentConnection self
  270.     )
  271. )
  272.  
  273. method startSharing self {object AnimationDemo} -> (
  274.     -- print "starting" 
  275.     self.animal.x := self.width
  276.     self.mw.state := @gaining
  277. )
  278.  
  279. method stillSharing self {object AnimationDemo} x -> (
  280.     -- print "still"
  281.     self.animal.x := x + self.width
  282.     self.mw.state := @gaining
  283. )
  284.  
  285. method gotback self {object AnimationDemo} -> (
  286.     -- print "still"
  287.     self.animal.x := self.width
  288.     self.mw.state := @lost
  289. )
  290. method startOwning self {object AnimationDemo} -> (
  291.     self.mw.state := @own
  292.     resume self.clock
  293.     if not (ismember self.mc self.animal) do
  294.         append self.mc self.animal
  295. )
  296.  
  297. method doQuit self {object AnimationDemo} -> (
  298.     plug self.mqueue
  299.     plug self.tcpStream
  300. )
  301.  
  302. global packetstates := #(@lost, @losing, @stilllosing, @gotback, @quit)
  303.  
  304. method writePacket self {object AnimationDemo} state x -> (
  305.     local byte1
  306.     local byte2
  307.     local byte3
  308.     
  309.     byte1 := getordone packetstates state
  310.     x := (-x) as integer
  311.     byte2 := mod x 256
  312.     byte3 := (x / 256) as integer
  313.  
  314.     write  self.currentConnection.mQueue #(byte1, byte2, byte3)
  315. )
  316.  
  317.  
  318. method handleNormalMessage self {object animationdemo} obj -> (
  319.     local packet := #(packetstates[obj[1]], -(obj[2] + (256 * obj[3])))
  320.     case packet[1] of
  321.         @lost : (
  322.             startowning self
  323.             return true
  324.             )
  325.         @losing : (
  326.             startsharing self
  327.             return false
  328.             )
  329.         @stilllosing :  (
  330.             stillSharing self packet[2]
  331.             return false
  332.             )
  333.         @gotback : (
  334.             gotback self
  335.             return false
  336.         )
  337.         @quit : (
  338.             handleQuitMessage self
  339.             return true
  340.         )
  341.     end
  342. )
  343.  
  344. method noteLostConnection self {object animationdemo} con -> (
  345.     if self.currentConnection = con do (
  346.         loseCurrentConnection self
  347.         noteLosing self
  348.     )
  349. )
  350.  
  351. method noteNewConnection self {object animationdemo} con isFirst -> (
  352.     if self.mw != undefined and self.mw.state = @standalone do
  353.         self.mw.state := @own
  354. )
  355.  
  356. method setState self {object animationdemo} state -> (
  357.     self.state := state
  358.     updateButtons self
  359. )
  360.  
  361. method updateButtons self {object animationdemo} -> (
  362.     self.quitButton.enabled := true
  363.     self.disconnectButton.enabled := true
  364. )
  365.  
  366.  
  367. method handleQuitMessage self {object animationdemo} -> (
  368.     -- Do nothing
  369. )
  370.  
  371. method userQuit self {object animationdemo} -> (
  372.     userDisconnect self
  373.     stopListening self.myCM
  374.     -- What else do you have to do?
  375.     removeFish self
  376.     hide self    
  377. )
  378.  
  379. method removeFish self {object animationdemo} -> (
  380.     -- This is not quite right
  381.     -- deleteOne self self.animal
  382. )
  383.  
  384.  
  385. method userDisconnect self {object animationdemo} -> (
  386.     disconnect self.myCM
  387. )
  388.  
  389. method createFish self {object animationdemo} -> (
  390.     startowning self
  391. )
  392.  
  393. method connectCallback self {object animationdemo} -> (
  394.     new fishui client: self getgoing: doConnect showserver: true
  395. )
  396.  
  397. method doConnect self {object animationdemo} #key state: server: serverport: port: -> (
  398.     connectToServer self.myCM server port
  399.     connectToSomeMore self.myCM
  400. )
  401.  
  402.  
  403.  
  404. -- End of class definition for AnimationDemo.
  405.